// npm babel-node array_demo01.js

// find

let test01 = function() {
    let _ = [1, 4, -5, 10].find((n) => n < 0)
    console.log(_)
};

// test01();

let test02 = function() {
    let _ = [1, 5, 10, 15].find(function(value, index, arr){
        return value > 9;
    })
    console.log(_)
}

// test02();

// findIndex

let test03 = function() {
    let _ = [1, 5, 10, 15].findIndex(function(value, index, arr){
        return value > 9;
    })
    console.log(_)
}

// test03()

// 这两个方法都可以接受第二个参数，用来绑定回调函数的this对象。

function f(v) {
    return v > this.age;
}

let test04 = function(){
    let person = {name: 'John', age: 20};
    let _ = [10, 12, 26, 15].find(f, person);
    console.log(_)
}

// test04()

// fill

let test05 = function() {
    let arr = new Array(3).fill({name: "Mike"});
    console.log(arr)
    arr[0].name = "Ben";
    console.log(arr)
}

// test05()

// 数组实例的 entries，keys 和 values

let test06 = function() {
    for (let index of ['a', 'b'].keys()) {
        console.log(index)
    }
}

// test06()

let test07 = function() {
    for (let elem of ['a', 'b'].values()) {
        console.log(elem)
    }
}

// test07()

let test08 = function() {
    for (let [index, elem] of ['a', 'b'].entries()) {
        console.log(index, elem)
    }
}

// test08()


let test09 = function() {
    let letter = ['a', 'b', 'c']
    let entries = letter.entries()
    console.log(entries.next().value)
    console.log(entries.next().value)
    console.log(entries.next().value)
}

// test09()

// 数组实例的 includes()

let test10 = function() {
    let _ = [1, 2, 3].includes(2)
    console.log(_)
    _ = [1, 2, 3].includes(4)
    console.log(_)
    _ = [1, 2, NaN].includes(NaN)
    console.log(_)
}

// test10()

// flat : 将多维数组降维

let test11 = function() {
    let _;

    // 默认拉平 1 层
    _ = [1, 2, [3, [4, 5]]].flat()
    console.log(_)

    // 可传递参数 2 
    _ = [1, 2, [3, [4, 5]]].flat(2)
    console.log(_)

    // 可传递参数 Infinity
    _ = [1, [2, [3, [4]]]].flat(Infinity)
    console.log(_)
}

// test11()

let test12 = function() {
    let _
    _ = [2, 3, 4].flatMap((x) => [x, x * 2])
    console.log(_)

    // flatMap 只能展开一层
    _ = [1, 2, 3, 4].flatMap( x => [[x * 2]])
    console.log(_)
}

// test12()

// 对空位的处理

 let test13 = function() {
     let _

     // 空位不是undefined
     _ = 0 in [undefined, undefined, undefined]
     console.log(_)
     _ = 0 in [, , ,]
     console.log(_)

    //  [, 'a'].forEach((x, i) => console.log(i))

    // _ ['a',,'b'].filter(x => true)
    console.log(_)

 }

 test13()















